package com.lgx.common.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.entity.IntraCity;
import com.lgx.common.exception.DataException;
import com.lgx.common.service.IIntraCityService;
import com.lgx.common.web.Constants;
import com.lgx.common.web.json.Json2Utils;
import com.lgx.common.web.json.JsonUtils;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.right.entity.User;

/**
 * 同城处理
 * 
 * @author acer
 *
 */
@Controller
@RequestMapping("/intra/city")
public class IntraCityController {

	@Autowired
	private IIntraCityService intraCityService;

	public IIntraCityService getIntraCityService() {
		return intraCityService;
	}

	public void setIntraCityService(IIntraCityService intraCityService) {
		this.intraCityService = intraCityService;
	}
	
	@RequestMapping("/intraCityMgr.do")
	public String intraCityMgr(HttpServletRequest request,HttpServletResponse response){
		return "common/city/intraCityMgr";
	}
	
	@RequestMapping("/list.do")
	public void list(HttpServletRequest request,HttpServletResponse response){
		List<IntraCity> ics = this.intraCityService.findIntraCityList();
		Json2Utils.parseJSON(ics, response);
	}
	
	@RequestMapping("/init.do")
	public String init(HttpServletRequest request,HttpServletResponse response){
		String type = request.getParameter("type");
		String cityId = request.getParameter("cityId");
		
		if("edit".equals(type)){
			IntraCity city = this.intraCityService.findIntraCityById(cityId);
			request.setAttribute("city", city);
		}
		
		request.setAttribute("type", type);
		request.setAttribute("cityId", cityId);
		
		return "common/city/intraCity-init";
	}
	
	@RequestMapping("/save.do")
	@ResponseBody
	public String save(HttpServletRequest request,HttpServletResponse response){
		
		User user = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
		
		String areaName = request.getParameter("areaName");
		if(!StringUtils.isEmpty(areaName))areaName = EscapeUtils.unescape(areaName);
		String areaNode = request.getParameter("areaNode");
		String areaId = request.getParameter("areaId");
		
		IntraCity city = new IntraCity();
		city.setAreaId(areaId);
		city.setAreaName(areaName);
		city.setAreaNode(areaNode);
		
		city.setCreateDate(new Date());
		city.setCreateName(user.getUsername());
		
		try {
			this.intraCityService.createIntraCity(city);
		} catch (DataException e) {
			e.printStackTrace();
			return "0";
		}
		
		return "1";
	}
	
	@RequestMapping("/update.do")
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response){
		String cityId = request.getParameter("cityId");
		IntraCity city = this.intraCityService.findIntraCityById(cityId);
		String areaName = request.getParameter("areaName");
		if(!StringUtils.isEmpty(areaName))areaName = EscapeUtils.unescape(areaName);
		String areaNode = request.getParameter("areaNode");
		String areaId = request.getParameter("areaId");
		
		city.setAreaId(areaId);
		city.setAreaName(areaName);
		city.setAreaNode(areaNode);
		
		try {
			this.intraCityService.updateIntraCity(city);
		} catch (DataException e) {
			e.printStackTrace();
			return "0";
		}
		
		return "1";
	}
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response){
		String ids = request.getParameter("ids");
		
		for(String id : ids.split(",")){
			try {
				this.intraCityService.deleteIntraCity(id);
			} catch (DataException e) {
				e.printStackTrace();
				return "0";
			}
		}
		return "1";
	}
	
	@RequestMapping("/listToCombobox.do")
	public void listToCombobox(HttpServletRequest request,HttpServletResponse response){
		List<IntraCity> ics = this.intraCityService.findIntraCityList();
		JsonUtils utils = new JsonUtils();
		String json;
		try {
			json = utils.converToJson(ics);
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
